from app import db
from sqlalchemy import orm

class BaseService:
    @classmethod
    def bulk_insert(cls, model, data_list):
        """批量插入优化"""
        db.session.bulk_insert_mappings(model, data_list)
        db.session.commit()

    @classmethod
    def bulk_update(cls, model, data_list):
        """批量更新优化""" 
        db.session.bulk_update_mappings(model, data_list)
        db.session.commit()

    @classmethod
    def query_by_ids(cls, model, ids):
        """IN查询优化"""
        return db.session.query(model).filter(model.id.in_(ids)).all()

    @classmethod
    def eager_load(cls, query, *relationships):
        """贪婪加载关联查询"""
        return query.options(*[orm.joinedload(rel) for rel in relationships])
